<template>
  <div class="app-container">


    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="deptList"
      row-key="deptId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="编号" width="260"></el-table-column>
      <el-table-column prop="vehicleName" label="车牌号" width="200"></el-table-column>

      <el-table-column prop="statusName" label="订单状态" width="200"></el-table-column>

      <el-table-column prop="payment" label="金额" width="200"></el-table-column>

      <el-table-column label="创建时间" align="center" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="payAli(scope.row)"
            v-hasPermi="['vehicle:edit']"
          >支付</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="payAlitukuan(scope.row)"
            v-hasPermi="['vehicle:edit']"
          >退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改部门对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编号" prop="carNumber">
              <el-input v-model="form.carNumber" placeholder="车牌号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="部门状态">
              <el-radio v-model="form.status" :label="0">启用</el-radio>
              <el-radio v-model="form.status" :label="1">禁用</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="续费状态">
              <el-radio v-model="form.monthlyStatus" :label="0">续费</el-radio>
              <el-radio v-model="form.monthlyStatus" :label="1">不续费</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="form.monthlyTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {addVehicle, detVehicle, listVehicle, updateVehicle} from "@/api/vehicle/vehicle";
import {listPayment, payMentRefund} from "@/api/payment/payment";

export default {
  name: "Dept",
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,

      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum:1,
        pageSize:5,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carNumber: [
          { required: true, message: "车牌不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // this.getList();
    this.listPayment();
  },
  methods: {
    /** 查询部门列表 */
    // getList() {
    //   this.loading = true;
    //   listDept(this.queryParams).then(response => {
    //     this.deptList = this.handleTree(response.data, "deptId");
    //     this.loading = false;
    //   });
    // },
    /** 查询车辆列表 */
    listPayment() {
      listPayment(this.queryParams).then(response => {
        console.log(response)
        this.deptList = this.handleTree(response.data.records, "id");
        this.loading = false;

      });
      console.log("ppp")
    },

    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: undefined,
        parentId: undefined,
        deptName: undefined,
        orderNum: undefined,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.listPayment();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.id = row.id;

      }
      this.open = true;
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      if (row != undefined) {
        this.form = row;
        this.form.status = row.status
        console.log(this.form,'-----')
      }
      this.open = true;
      console.log(this.dict.type)

    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            console.log(this.form,'----')
            updateVehicle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.listPayment();
            });

          } else {
            console.log("新增")
            console.log(this.form,'----')
            addVehicle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.listPayment();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.carNumber + '"的数据项？').then(function() {
        return detVehicle(row);
      }).then(() => {
        this.listPayment();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    payAli(row){
      console.log(row)
      window.open("http://localhost:9201/alipay/pay?orderNo="+row.id)
    },
    payAlitukuan(row) {
      this.$confirm('是否确认退款金额为"' + row.payment + '"元的订单？', "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return payMentRefund(row);
      }).then(() => {
        this.listPayment();
        this.$message.success("退款成功");
      }).catch(() => {});
    }
  }
};
</script>
